<?php

namespace App\Admin\Models\Financial;

use App\Admin\Models\AdminPaytypeList;
use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class FinancialRate extends Model
{
    protected $table = 'admin_financial_rate';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList(Request $request)
    {
        $query = self::with(['paytypeinfo'])->where(['is_del' => 1])->orderBy('create_at', 'desc');
        $pay_type_id=intval($request->pay_type_id);
        if($pay_type_id>0){
            $query->where(['pay_type_id'=>$pay_type_id]);
        }

        //费率类型
        $val_type = $request['val_type'] ?? 0;
        if ($val_type > 0) {
            $query->where('val_type', $val_type);
        }

        $isgetall = intval($request->isgetall) ?? 0;

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function paytypeinfo(){
        return $this->hasOne(AdminPaytypeList::class,'id','pay_type_id');
    }
}
